<?php

class App_Model_DbTable_Article_Tags extends Ext_Db_Table  
{  
    /** Table name */  
    protected $_name = 'article_tags';
    
    private $_tagRow = array();
    
    public function getRowById($tagId)
    {        
        if(!$tagId)
        {
            return "";
        }
        
        if(isset($this->_tagRow[$tagId]))
        {
            return $this->_tagRow[$tagId];
        }
        
        $db = $this->getAdapter();
        $safe = $db->quoteInto(" id = ? ", $tagId);
        $row = $this->fetchRow($safe);
        
        if (!$row) {
            return "";
        }
        $_tagRow = $row->toArray();
        
        $this->_tagRow[$tagId] = $_tagRow;
        
        return $_tagRow;
    }
    
    public function add($tags, $articleId)
    {
        $_articleTags = $this->getRowById($articleId);
        if($_articleTags)
        {
            $row = array (
                'words'         => $tags,
            );
            try {
                $db = $this->getAdapter();
                $where = $db->quoteInto(" id = ? ", $articleId);
                $rows_affected = $this->update($row, $where);
            }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
            }
        }
        else
        {
            $row = array (
                'id'             => $articleId,
                'words'         => $tags,
            );
            try {
                // 插入数据行并返回插入的行数
                $rows_affected = $this->insert($row);
            }catch (Exception $e){
                trigger_error($e->getMessage(), E_USER_WARNING);
            }
        }
        
        return "";
    }
}  

